import { Global, Module } from '@nestjs/common';
import { createClient } from 'redis';

import { ConstantEnum } from '../enums';
import { getConfig } from '../utils';
import { RedisService } from './redis.service';

// 设置数据库类型
const { REDIS_CONFIG } = getConfig();

@Global()
@Module({
  providers: [
    RedisService,
    {
      provide: ConstantEnum.REDIS_CLIENT,
      useFactory: async () => {
        const client = createClient({
          socket: {
            host: REDIS_CONFIG.host,
            port: REDIS_CONFIG.port,
          },
          password: REDIS_CONFIG.password,
          database: REDIS_CONFIG.db,
        });
        await client.connect();
        return client;
      },
    },
  ],
  exports: [RedisService],
})
export class RedisModule { }
